package com.thinkgem.jeesite.modules.oa.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.oa.entity.OaOfficicalAccounts;
import com.thinkgem.jeesite.modules.oa.service.OaOfficialAccountsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Created by LZY on 2018/2/24.
 */
@Controller
@RequestMapping(value = "${adminPath}/oa/OfficialAccounts")
public class OaOfficialAccountsContronller extends BaseController {

    @Autowired
    private OaOfficialAccountsService oaOfficialAccountsService;

    @ModelAttribute
    public OaOfficicalAccounts get(@RequestParam(required=false) String id){
        OaOfficicalAccounts accounts = null;
        if (StringUtils.isNotBlank(id)){
            accounts = oaOfficialAccountsService.getById(id);
        }
        if (accounts == null){
            accounts = new OaOfficicalAccounts();
        }
        return accounts;
    }

    @RequestMapping(value = {"list", ""})
    public String list(OaOfficicalAccounts oaOfficicalAccounts, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<OaOfficicalAccounts> page = oaOfficialAccountsService.find(new Page<OaOfficicalAccounts>(request, response), oaOfficicalAccounts);
        model.addAttribute("page", page);
        model.addAttribute("accounts",oaOfficicalAccounts);
        return "modules/oa/accountsList";
    }

    @RequestMapping(value = "form")
    public String form(OaOfficicalAccounts oaOfficicalAccounts, Model model) {
        if (StringUtils.isNotBlank(oaOfficicalAccounts.getId())){
            oaOfficicalAccounts = oaOfficialAccountsService.get(oaOfficicalAccounts.getId());
        }
        model.addAttribute("accounts", oaOfficicalAccounts);
        return "modules/oa/accountsForm";
    }

    @RequestMapping(value = "save")
    public String save(OaOfficicalAccounts oaOfficicalAccounts, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, oaOfficicalAccounts)){
            return form(oaOfficicalAccounts, model);
        }
        oaOfficialAccountsService.save(oaOfficicalAccounts);
        addMessage(redirectAttributes, "保存文章'" + oaOfficicalAccounts.getTitle() + "'成功");
        return "redirect:" + adminPath + "/oa/OfficialAccounts/?repage";
    }

}
